package com.example.myapplication;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;


import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    Button exitbtn;
    Button addbtn;
    Button setbtn;
    Button searchbtn;
    List<Thing> thingList=new ArrayList<>();
    ThingAdapter adapter;


    private UserDAO dao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        dataUpdate();
        adapter=new ThingAdapter(MainActivity.this,R.layout.thinglabel,thingList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int thingid=thingList.get(i).getId();
                Intent intent=new Intent(MainActivity.this, AddAndEditActivity.class);
                Bundle b=new Bundle();
                b.putSerializable("data",(Serializable) thingid);
                intent.putExtras(b);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        dataUpdate();
        if(adapter!=null){
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
        }
    }

    private void init() {
        dao=new UserDAO(this);
        if(dao.queryIsFirstUse()){
            dao.updateIsFurstUse(false);
            dao.initUnit();
            dao.initTypeFirst();
        }
        exitbtn=findViewById(R.id.exitbtn);
        addbtn=findViewById(R.id.addbtn);
        setbtn=findViewById(R.id.setbtn);
        searchbtn=findViewById(R.id.searchbtn);
        listView=(ListView) findViewById(R.id.itemlist);
        exitbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dao.closeDB();
                finish();
            }
        });

        addbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this, AddAndEditActivity.class);
                startActivity(intent);
            }
        });
        //暂时用于测试
        setbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this, SetActivity.class);
                startActivity(intent);
            }
        });
        searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });
        Log.d("输出", "init: 主界面初始化完成");
    }

    //数据在这里获取
    private void dataUpdate() {
        thingList.clear();
        thingList.addAll(dao.queryThingAll());
        Log.d("输出", "dataUpdate: 数据更新"+thingList.size());
    }
}